import 'package:enterprise_v6_app/app/components/common/custom_no_data.dart';
import 'package:enterprise_v6_app/app/components/dialing_function.dart';
import 'package:enterprise_v6_app/app/components/organize_people_tree/organize_people_tree_controller.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/tree_node_bean.dart';
import 'package:enterprise_v6_app/app/data/common/tree_node_child_bean.dart';

/// 组织人员树状显示
class OrganizePeopleTreePage<T1 extends TreeNodeBean, T2 extends TreeNodeChildBean> extends StatelessWidget {
  OrganizePeopleTreePage({
    super.key,
    this.organizeInterface,
    this.peopleInterface,
    this.queryParameters,
    this.nodeParameterName,
  });

  /// 查询组织接口
  final String? organizeInterface;

  /// 查询人员接口
  final String? peopleInterface;

  /// 树节点组织请求参数
  final Map<String, dynamic>? queryParameters;

  /// 子节点请求参数名
  final String? nodeParameterName;

  final controller = Get.put(OrganizePeopleTreeController<T1, T2>());

  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.topCenter,
        color: const Color(0xffffffff),
        child: GetBuilder(
            initState: (state) {
              controller.init(
                organizeInterface: organizeInterface,
                peopleInterface: peopleInterface,
                queryParameters: queryParameters,
                nodeParameterName: nodeParameterName,
              );
            },
            init: controller,
            builder: (_) {
              return controller.departmentTreeList.isEmpty
                  ? const CustomNoData()
                  : SingleChildScrollView(
                      child: Column(
                      children: controller.departmentTreeList.map((item) {
                        return _getTreeView(item, 0);
                      }).toList(),
                    ));
            }));
  }

  /// 部门数据每一项的显示
  /// [treeEntity] 当前节点数据
  /// [level] 所在层级
  Widget _getTreeView(T1 treeEntity, int level) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          constraints: BoxConstraints(
            minHeight: 55.w,
          ),
          padding: EdgeInsets.symmetric(vertical: 10.w),
          child: Row(
            children: [
              // 间距
              SizedBox(width: 20.w * level),
              SizedBox(
                width: 40.w,
                child: SvgPicture.asset(
                  treeEntity.isExpand ? AppImages.icon_arrow_drop : AppImages.icon_arrow_right,
                  width: 12.w,
                  height: 12.w,
                ),
              ),
              Expanded(
                child: Text(
                  treeEntity.getShowName(),
                  style: TextStyle(
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w500,
                    color: const Color(0xff333333),
                  ),
                ),
              ),
            ],
          ),
        ).clickOnce(() {
          controller.clickOnNode(treeEntity);
        }),
        Divider(
          height: 0.1.w,
          color: const Color(0xffDDE6F0),
        ),
        AnimatedSize(
          duration: const Duration(milliseconds: 500),
          // curve: Curves.easeInExpo,
          curve: Curves.easeInOutExpo,
          child: SizedBox(
            height: treeEntity.isExpand ? null : 0,
            child: Column(
              children: [
                // 组织部门
                ...treeEntity.children.map((item) {
                  return _getTreeView(item as T1, level + 1);
                }),
                // 人员
                ...treeEntity.userList.map((item) {
                  return _getUserTreeView(item as T2, level + 1);
                })
              ],
            ),
          ),
        ),
      ],
    );
  }

  /// 部门下人员信息
  /// [userEntity] 人员数据
  /// [level] 所在层级
  Widget _getUserTreeView(T2 userEntity, int level) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          constraints: BoxConstraints(
            minHeight: 55.w,
          ),
          padding: EdgeInsets.symmetric(vertical: 10.w),
          child: Row(
            children: [
              // 间距
              SizedBox(width: 20.w * level),
              // 占位
              SizedBox(width: 40.w),
              Expanded(
                child: Text(
                  '${userEntity.getShowName()}\n${userEntity.getShowPhone()}',
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff333333).withOpacity(0.5),
                  ),
                ).clickOnce(() {
                  if (userEntity.getShowPhone().isNotEmpty) {
                    DialingFunction.selectDialingBottomSheet(dataList: [userEntity.getShowPhone()]);
                  }
                }),
              ),
              // 分割线
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.w),
                child: Text(
                  '|',
                  style: TextStyle(
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w500,
                    color: const Color(0xffDDE6F0),
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(right: 20.w),
                child: Text(
                  userEntity.getShowRole(),
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff495061),
                  ),
                ),
              )
            ],
          ),
        ),
        Divider(
          height: 0.1.w,
          color: const Color(0xffECECEC),
        ),
      ],
    );
  }

}
